package org.streets.eis.component.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class DataModel<T>  implements Serializable{

	private static final long serialVersionUID = 1L;
	
	private T object = null;
	
	private DataModel<T> parent = null;
	
	private List<DataModel<T>> children = new ArrayList<DataModel<T>>();

	public DataModel(){
		this.object = null;
	}
	
	public DataModel(T object){
		this.object = object;
	}
	
	public T getObject() {
		return object;
	}

	public void setObject(T object) {
		this.object = object;
	}

	/**
	 * Don't use this method to add Child, Use addChild instead of this 
	 * @return children list
	 */
	public List<DataModel<T>> getChildren(){
		return children;
	}
	
	public DataModel<T> getChild(int index){
		return children.get(index);
	}
	
	public int getChildrenCount() {
		return children.size();
	}
	
	public DataModel<T> addChild(DataModel<T> child){
		this.children.add(child);
		child.setParent(this);
		return this;
	}
	
	public DataModel<T> removeChild(DataModel<T> child){
		child.setParent(null);
		this.children.remove(child);
		return this;
	}	

	public DataModel<T> removeChild(int index){
		this.children.remove(index).setParent(null);
		return this;
	}	

	public void setParent(DataModel<T> parent) {
		this.parent = parent;
	}

	public DataModel<T> getParent() {
		return parent;
	}

	public int getIndexOfParent(){
		if (parent!=null){
			return parent.getChildren().indexOf(this);
		}
		return -1;
	}
	
	public boolean isRoot(){
		return this.object == null;
	}
}
